﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace 控制台编码切换器
{
    internal static class Program
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern uint GetConsoleOutputCP();

        [STAThread]
        static void Main()
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            // 强制 Console UTF-8
            Console.OutputEncoding = Encoding.UTF8;
            Console.InputEncoding = Encoding.UTF8;

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("=== 控制台编码切换器 启动日志 ===");
            Console.ResetColor();

            uint cp = GetConsoleOutputCP();
            Console.WriteLine($"[INFO] 当前控制台编码页：{cp} ({Encoding.GetEncoding((int)cp).EncodingName})");

            bool isWindowsTerminal = Environment.GetEnvironmentVariable("WT_SESSION") != null;
            Console.WriteLine($"[INFO] 运行环境：{(isWindowsTerminal ? "Windows Terminal" : "传统 cmd.exe")}");

            if (isWindowsTerminal)
                Utils.ModifyWindowsTerminalSettings();
            else
                Utils.ModifyRegistryForCmd();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("你好，世界！🌍 中文、Emoji 正常输出 ✅");
            Console.ResetColor();

            ApplicationConfiguration.Initialize();
            Application.Run(new MainForm());
        }
    }
}
